package com.ruoyi.common.satoken.seller.filter;

import cn.hutool.core.collection.CollUtil;
import com.catering.common.seller.context.SellerContextHolder;
import com.ruoyi.common.satoken.utils.LoginHelper;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * 商家过滤器
 *
 * @author Administrator
 * @date 2023/02/27
 */
public class SellerFilter extends OncePerRequestFilter {

    private static final Set<String> ignoreUrls = CollUtil.newHashSet("/login");

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (!CollUtil.contains(ignoreUrls, request.getRequestURI())) {
            Long sellerId = LoginHelper.getSellerId();
            // 设置
            if (sellerId != null) {
                SellerContextHolder.setSellerId(sellerId);
            }
        }
        try {
            filterChain.doFilter(request, response);
        } finally {
            // 清理
            SellerContextHolder.clear();
        }
    }
}
